package dk.hewison.client.slideshow;


import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;
import org.adamtacy.client.ui.NEffectPanel;
import org.adamtacy.client.ui.effects.NEffect;
import org.adamtacy.client.ui.effects.NEffectHandlerAdapter;
import org.adamtacy.client.ui.effects.impl.Fade;

/**
 * @author John Hewison
 * @author $LastChangedBy: john.hewison $:  $ $LastChangedDate: 2009-02-09 12:39:39 +0000 (Mon, 09 Feb 2009) $:
 * @version $Revision: 363 $:
 */
public class WidgetEffectPanel extends Composite {

    private static final double FADE_TIME_SEC = 0.1;

    private Fade fadeIn;
    private Fade fadeOut;

    NEffectPanel effectPanel;

    TransitionCallback transitionCallback;

    public WidgetEffectPanel(Widget widget) {
        //image.setPixelSize(Window.getClientWidth(), Window.getClientHeight());

        effectPanel = new NEffectPanel();
        effectPanel.add(widget);
        //effectPanel.setEffectsLength(1.0);
        //add(effectPanel);
        initEffect();

        effectPanel.addEffect(fadeIn);


        initWidget(effectPanel);


    }


    public void fadeIn() {
        //fadeIn.play();
        effectPanel.playEffects();

    }

    public void fadeOut(final TransitionCallback callback) {
        fadeOut.play();
        if (callback != null) {
            fadeOut.addEffectHandler(new NEffectHandlerAdapter() {
                @Override
                public void postEvent(NEffect theEffect) {
                    callback.transtionFinished();
                }
            });
        }
        //effectPanel.playEffects();
    }

    public void initEffect() {
        fadeIn = new MyFade();
        fadeIn.getProperties().setStartValue("0");
        fadeIn.getProperties().setEndValue("100");
        //fadeIn.init(effectPanel);


        fadeOut = new MyFade();
        fadeOut.getProperties().setStartValue("100");
        fadeOut.getProperties().setEndValue("20");
        fadeOut.init(effectPanel);


    }


    /*@Override
    public void setPixelSize(int width, int height) {
        super.setPixelSize(width, height);
        double w1 = (double) width / (double) image.getWidth();
        double h1 = (double) height / (double) image.getHeight();

        int h = (int) (w1 * image.getHeight());
        if (h <= height) {
            image.setPixelSize(width, h);
        } else {
            int w = (int) (h1 * image.getWidth());
            image.setPixelSize(w, height);
        }

    }*/

    class MyFade extends Fade {


        MyFade() {
            effectLengthSeconds = FADE_TIME_SEC;
        }
    }
}

